type FunctionArgs<Args extends any[] = any[], Return = void> = (...args: Args) => Return;

/**
 * 节流原理：在一定时间内，只能触发一次
 * @param func - 要执行的回调函数
 * @param wait - 延时的时间
 * @param immediate - 是否立即执行
 */
// 节流(合并)
export function throttle(func: FunctionArgs, wait = 500, immediate = false) {
  let flag = true;

  if (immediate) {
    // eslint-disable-next-line func-names
    return function (...args: any[]) {
      if (flag) {
        func(...args);
        flag = false;
        setTimeout(() => {
          flag = true;
        }, wait);
      }
    };
  } else {
    // eslint-disable-next-line func-names
    return function (...args: any[]) {
      if (flag === true) {
        flag = false;
        setTimeout(() => {
          func(...args);
          flag = true;
        }, wait);
      }
    };
  }
}
